import { configureStore, createSlice } from "@reduxjs/toolkit";
import { getUser, getToken } from "../utils/local";
import { setToken, setUser } from "@/utils/local";

export const storeStateSlice = createSlice({
  name: "storeState",
  initialState: {
    theme: true,
    token: getToken(),
    userInfo: getUser(),
    layers: {
      query: {},
      name: "",
      other: {},
    },
    layersLevel2: {
      query: {},
      name: "",
      other: {},
    },
    area: {
      ids: [],
      names: [],
    }, // 省级区域
    year: "", // 年份
    month: "", // 月份
    productLine: "", // 产品线
    date: "", // 日期
    refresh: "", // 全局刷新
  },
  reducers: {
    setArea(state, action) {
      state.area = action.payload;
    },
    setYear(state, action) {
      state.year = action.payload;
    },
    setMonth(state, action) {
      state.month = action.payload;
    },
    setProductLine(state, action) {
      state.productLine = action.payload;
    },
    setDate(state, action) {
      state.date = action.payload;
    },
    setLayersLevel2(state, action) {
      state.layersLevel2 = action.payload;
    },
    setLayers(state, action) {
      state.layers = action.payload;
    },
    setTheme(state, action) {
      state.theme = action.payload;
    },
    storeSetToken(state, action) {
      state.token = action.payload;
      action.payload && setToken(action.payload);
    },
    storeSetUserInfo(state, action) {
      state.userInfo = action.payload;
      action.payload && setUser(action.payload);
    },
  },
});
export const {
  setTheme,
  setLayers,
  storeSetToken,
  setLayersLevel2,
  storeSetUserInfo,
  setArea,
  setYear,
  setMonth,
  setProductLine,
  setDate,
} = storeStateSlice.actions;
export default configureStore({
  reducer: storeStateSlice.reducer,
});
